package com.example.day02.not;

import java.util.concurrent.TimeUnit;

import okhttp3.OkHttpClient;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;

public class HttpUlist {

    private final Retrofit.Builder retrofit;

    public HttpUlist(){
        OkHttpClient okHttpClient = getOkHttpClient();
        retrofit = getRetrofit(okHttpClient);
    }
    public static volatile HttpUlist instance;
    public static HttpUlist getInstance(){
        if (instance==null){
            synchronized (HttpUlist.class){
                if (instance==null){
                    instance=new HttpUlist();
                }
            }
        }
        return instance;
    }
    private Retrofit.Builder getRetrofit(OkHttpClient okHttpClient) {
        return new Retrofit.Builder()
                .client(okHttpClient)
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .addConverterFactory(GsonConverterFactory.create());
    }

    private OkHttpClient getOkHttpClient() {
        return new OkHttpClient.Builder()
                .readTimeout(60, TimeUnit.SECONDS)
                .connectTimeout(60,TimeUnit.SECONDS)
                .writeTimeout(60,TimeUnit.SECONDS)
                .retryOnConnectionFailure(true)
                .build();
    }
    public synchronized <T> T getApiserver(String url,Class<T> tClass){
        return retrofit.baseUrl(url).build().create(tClass);
    }
}
